<?php
/**
 * Created by littleU.
 * User: littleU
 * Date: 2021-02-02
 * Time: 10:52
 */

namespace app\api\service;

use app\api\model\AdminRole as AdminRoleModel;
use app\api\model\AdminRoleMenu as AdminRoleMenuModel;
use app\api\model\AdminMenu as AdminMenuModel;


class AdminRole
{
    public static function getRoleList()
    {
        $roles = (new AdminRoleModel())->where('status',config('code.normal'))->select();
        return json([
            'code' => config('code.success'),
            'msg'  => '请求成功',
            'data' => $roles
        ]);
    }

    public static function getRoleMenuById($id)
    {
        $data = [];
        $checkIds = (new AdminRoleMenuModel())->where('role_id', $id)->where('status',config('code.normal'))->column('menu_id');
        $menus = (new AdminMenuModel())->where('status', config('code.normal'))->select();
        $data['list'] = $menus;
        $data['checkedId'] = $checkIds;
        return json([
            'code' => config('code.success'),
            'msg' => '数据获取成功',
            'data' => $data
        ]);
    }

    public function setRole($data)
    {
        $id = $data['id'];
        if($id == '')
        {
            return $this->addRole($data);
        }
        else
        {
            return $this->updateRole($data, $id);
        }
    }
    private function addRole($data)
    {
        try
        {
            $roleObj = new AdminRoleModel();
            $roleObj->name = $data['name'];
            $roleObj->save();
        }
        catch(\Exception $ex)
        {
            return json([
                'code' => config('code.error'),
                'msg' => $ex->getMessage()
            ]);
        }
        $roleId = $roleObj->id;
        $menuIds = [];
        foreach($data['authids'] as $k => $authid)
        {
            $item= [];
            $item['role_id'] = $roleId;
            $item['menu_id'] = $authid;
            array_push($menuIds, $item);
        }
        try
        {
            $roleMenuObj = new AdminRoleMenuModel();
            $roleMenuObj->saveAll($menuIds);
        }
        catch(\Exception $ex)
        {
            return json([
                'code' => config('code.error'),
                'msg' => $ex->getMessage()
            ]);
        }
        return json([
            'code' => config('code.success'),
            'msg' => '角色新增成功'
        ]);
    }

    private function updateRole($data, $id)
    {
        try {
            $role = AdminRoleModel::get($id);
            $role->name = $data['name'];
            $role->status = $data['status'];
            $role->save();
        }
        catch(\Exception $ex)
        {
            return json([
                'code' => config('code.error'),
                'msg' => $ex->getMessage()
            ]);
        }
        if(isset($data['authids']) && !empty($data['authids']))
        {
            $roleMenus = (new AdminRoleMenuModel())->where('role_id', $id)->select();
            foreach($roleMenus as $roleMenu)
            {
                $roleMenu->status = config('code.del');
                $roleMenu->save();
            }
            $menuIds = [];
            foreach($data['authids'] as $k => $authid)
            {
                $item = [];
                $item['role_id'] = $id;
                $item['menu_id'] = $authid;
                array_push($menuIds, $item);
            }
            try
            {
                $roleMenuObj = new AdminRoleMenuModel();
                $roleMenuObj->allowField(true)->saveAll($menuIds);
            }
            catch(\Exception $ex)
            {
                return json([
                    'code' => config('code.error'),
                    'msg' => $ex->getMessage()
                ]);
            }
        }
        return json([
            'code' => config('code.success'),
            'msg' => '角色编辑成功'
        ]);
    }
}